package org.spp.voronoi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Facet.java
 * @author Ilio Catallo, Eleonora Ciceri
 * @version 1.0
 * 
 * Description: the class implements the facet between two cells in case both
 *              of tridimensional and bidimensional vector spaces. A facet can
 *              also be in common between a cell and the bounded region
 * 
 * Date: Jul 10, 2011
 */
public abstract class Facet {
    
    // Attributes
    protected HashMap cells;
    protected boolean inBoundedRegion;
    protected double[] coefficients;
    
    /**
     * Constructor for the class Facet
     */
    public Facet(){
        this.cells = new HashMap();
        this.inBoundedRegion = false;
    }
    
    /**
     * Constructor for the class Facet
     * @param inBoundedRegion 
     */
    public Facet(boolean inBoundedRegion){        
        this();
        this.inBoundedRegion = inBoundedRegion;        
    }
    
    /**
     * Constructor for the class Facet
     * @param inBoundedRegion 
     */
    public Facet(double[] coefficients, boolean inBoundedRegion){        
        this(inBoundedRegion);
        this.coefficients = coefficients;       
    }    
    
    /**
     * Constructor for the class Facet
     * @param firstCentroid
     * @param secondCentroid 
     */
    public Facet(Centroid firstCentroid, Centroid secondCentroid) {
        
        this();
        
        // Extract the cells
        Cell firstCell = firstCentroid.getCell();
        Cell secondCell = secondCentroid.getCell();
       
        // Populate the HashMap with the centroids' cells
        cells.put(firstCell, -1);
        cells.put(secondCell, 1);
    }
    
    /**
     * Constructor for the class Facet
     * @param firstCentroid
     * @param secondCentroid
     * @param inBoundedRegion 
     */
    public Facet(Centroid firstCentroid, Centroid secondCentroid, boolean inBoundedRegion){
        
        this(firstCentroid, secondCentroid);
        this.inBoundedRegion = inBoundedRegion;
    }
    
    /**
     * Method that returns the cells that share the facet
     * @return cells
     */
    public List<Cell> getCells(){
        List<Cell> cells = new ArrayList<Cell>(this.cells.keySet());
        return cells;
    }
    
    /**
     * Method that returns the sign of the normal vector of the facet wrt
     * a given cell
     * @param cell
     * @return sign
     */
    public int getSign(Cell cell) {
        return Integer.parseInt(cells.get(cell).toString());
    }
    
    /**
     * Method that returns the coefficients of the plane
     * @return coefficients
     */
    public double[] getCoefficients() {
        return coefficients;
    }

    /**
     * Method that sets the coefficients of the plane
     * @param coefficients 
     */
    public void setCoefficients(double[] coefficients) {
        this.coefficients = coefficients;
    }     
    
    /**
     * Method that checks if the facet belongs to the given cell
     * @param cell
     * @return isFacetInCell
     */
    public boolean isFacetInCell(Cell cell) {
        return cells.containsKey(cell);
    }
    
    /**
     * Method that check if the facet is tagged as part of the bounded region
     * @return inBoundedRegion
     */
    public boolean isInBoundedRegion() {
        return inBoundedRegion;
    }
    
    public void addCell(Cell cell, int coefficient) {
        cells.put(cell, coefficient);
    }
    
    public void removeCell(Cell cell) {
        cells.remove(cell);
    }
    
}
